﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CalculateNormal
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //接收结果
            string Text;
            //接收三点坐标
            string[] A = textBox1.Text.Split(',', '，');
            string[] B = textBox2.Text.Split(',', '，');
            string[] C = textBox3.Text.Split(',', '，');
            //判断输入的三点坐标格式是否正确，不正确重新输入坐标
            if (A.Length != 3 || B.Length != 3 || C.Length != 3)
            {
                textBox4.Text = "坐标格式错误，请重新输入坐标！！";
                return;
            }
            //坐标装到集合中循环遍历验证格式是否正确，不正确重新输入坐标
            ArrayList array = new ArrayList();
            array.Add(A[0]);
            array.Add(A[1]);
            array.Add(A[2]);
            array.Add(B[0]);
            array.Add(B[1]);
            array.Add(B[2]);
            array.Add(C[0]);
            array.Add(C[1]);
            array.Add(C[2]);
            foreach (string item in array)
            {
                if (RegularExpression(item) != true)
                {
                    textBox4.Text = "坐标格式错误，请重新输入坐标！！";
                    return;
                }
            }
            //计算AB,BC,AC的向量，终点减起点
            float vectorABx = float.Parse(B[0]) - float.Parse(A[0]);
            float vectorABy = float.Parse(B[1]) - float.Parse(A[1]);
            float vectorABz = float.Parse(B[2]) - float.Parse(A[2]);
            float vectorACx = float.Parse(C[0]) - float.Parse(A[0]);
            float vectorACy = float.Parse(C[1]) - float.Parse(A[1]);
            float vectorACz = float.Parse(C[2]) - float.Parse(A[2]);
            if (Collinear(vectorABx, vectorABy, vectorABz, vectorACx, vectorACy, vectorACz))
            {
                MessageBox.Show("三点坐标共线", "错误");
                return;
            }
            //计算法线向量
            NormalVector(vectorABx, vectorABy, vectorABz, vectorACx, vectorACy, vectorACz);
        }
        //计算法线向量
        void NormalVector(float vectorABx, float vectorABy, float vectorABz, float vectorACx, float vectorACy, float vectorACz)
        {
            //AB*AC叉积    y1z2-z1y2  z1x2-x1z2  x1y2-x2y1
            //两个向量的叉积与这两个向量和垂直    右手螺旋法则
            double NormalVectorX, NormalVectorY, NormalVectorZ;
            NormalVectorX = (vectorABy * vectorACz) - (vectorABz * vectorACy);
            NormalVectorY = (vectorABz * vectorACx) - (vectorABx * vectorACz);
            NormalVectorZ = (vectorABx * vectorACy) - (vectorACx * vectorABy);
            textBox4.Text = NormalVectorX + "," + NormalVectorY + "," + NormalVectorZ;
        }

        //判断三点是否共线
        bool Collinear(float vectorABx, float vectorABy, float vectorABz, float vectorACx, float vectorACy, float vectorACz)
        {
            if (vectorABx * vectorACy - vectorACx * vectorABy == 0 && vectorABx * vectorACz - vectorACx * vectorABz == 0 && vectorABy * vectorACz - vectorACy * vectorABz == 0)
            {
                return true;
            }
            return false;
        }
        //判断文本框出入的格式是否正确；
        bool RegularExpression(string y)
        {
            bool Expression = Regex.IsMatch(y, @"(^-?[0-9]{1,}$)|(^-?[0-9]{1,}\.{1}[0-9]{1,}$)");
            return Expression;
        }
    }
}
